package com.sjtu.rental.form;

import org.apache.commons.validator.GenericValidator;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.sjtu.rental.entity.UserInfo;

@Component("registerForm")
public class RegisterForm implements Validator {

	private UserInfo user;

	public boolean supports(Class<?> arg0) {
		return RegisterForm.class.equals(arg0);
	}

	public void validate(Object o, Errors e) {
		UserInfo user = ((RegisterForm) o).getUser();
		if (GenericValidator.isBlankOrNull(user.getUiUsername())) {
			e.rejectValue("user.uiUsername", "login.username.notNull");
		}
		if (GenericValidator.isBlankOrNull(user.getUiPassword())) {
			e.rejectValue("user.uiPassword", "login.password.notNull");
		}
		if (GenericValidator.isBlankOrNull(user.getUiEmail())) {
			e.rejectValue("user.uiEmail", "login.email.notNull");
		} else if (!GenericValidator.isEmail(user.getUiEmail())) {
			e.rejectValue("user.uiEmail", "login.email.invalid");
		}
	}

	// getter and setter

	public UserInfo getUser() {
		return user;
	}

	public void setUser(UserInfo user) {
		this.user = user;
	}

}
